package tasks

import (
	"ddns/tencent"
	"ddns/utils"
	"log"
)

var cacheValue = ""

func Run() {
	ip, _ := utils.GetIp()
	if ip == cacheValue {
		// ip同缓存无变化
		return
	}

	describeRecordResponse := tencent.DescribeRecord()
	if describeRecordResponse == nil || describeRecordResponse.Response.RecordInfo == nil {
		log.Println("============获取解析记录失败============")
		return
	}

	oldValue := *describeRecordResponse.Response.RecordInfo.Value

	if oldValue == ip {
		// ip同远程无变化
		cacheValue = ip
	} else {
		log.Println("============ip由{}变化为{}============", oldValue, ip)
		modifyRecordResponse := tencent.ModifyRecord(ip)
		if modifyRecordResponse == nil || modifyRecordResponse.Response.RecordId == nil {
			log.Println("============更新解析记录失败============")
			return
		}
		cacheValue = ip
	}
}
